let express = require("express")();
let expressStatic = require("express-static");
let mysql = require("mysql");
let bodyParser = require("body-parser");
let port = 81;

//为body-parser进行配置
express.use(bodyParser.json());
express.use(bodyParser.urlencoded({ extended: false }));

//规划数据库
let sql = mysql.createConnection({
    host: 'localhost',
    password: '123456',
    user: 'root',
    database: 'coach',
    timezone: '08:00'
})

//连库
sql.connect();

//接收前端登录请求
express.post("/login", (request, response) => {
    let l = request.body;
    sql.query(`SELECT * FROM users WHERE username="${l.username}" AND password="${l.password}" `, (error, data) => {
        if (error) {
            response.send("3");
            console.log(error)
        } else {
            if (data.length === 0) {
                response.send("2");
            } else {
                response.send(data);
            }
        }
    })
})

//接收前端注册请求
express.post("/register", (request, response) => {
    let r = request.body;
    sql.query(`INSERT INTO users (username,password,name) VALUES("${r.username}","${r.password}","${r.name}") `, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send("success");
            console.log(`${r.name}用户注册成功`)
        }
    })
})

//接收前端列表页的请求
express.get("/getList", (request, response) => {
    //查库
    sql.query(`SELECT id,img,title,price FROM list`, (error, data) => {
        response.send(data);
    })
})

//接收前端详情页的请求
express.get("/details", (request, response) => {
    sql.query(`SELECT * FROM list WHERE id="${request.query.id}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data[0]);
        }
    })
})


// 接收前端查库请求
express.get("/getShop", (request, response) => {
    sql.query(`SELECT * FROM shop`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data[0]);
        }
    })
})


//接收购物车结算商品的请求
express.get("/addShop", (request, response) => {
    let a = request.query;
    sql.query(`INSERT INTO shop (id,name,count,totalprice) VALUES ("${a.id}","${a.name}","${a.count}","${a.totalprice}")`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send("success");
            console.log(`添加商品${a.name}成功`)
        }
    })
})

//接收修改购物车的请求
express.get("/editShop", (request, response) => {
    let e = request.query;
    sql.query(`UPDATE shop SET count = "${e.count}",totalprice = "${e.totalprice}" WHERE id="${e.id}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send("success");
            console.log(`修改购买${e.id}号商品成功`)
        }
    })
})

//接收结算订单详情的请求
express.get("/shopDetails", (request, response) => {
    sql.query(`SELECT * FROM shop WHERE id="${request.query.id}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data[0]);
        }
    })
})

//接收前端查看用户信息请求
express.post("/user", (request, response) => {
    let u = request.body;
    sql.query(`SELECT * FROM users WHERE username="${u.username}" AND password="${u.password}" AND name="${u.name}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            if (data.length === 0) {
                response.send("错误");
            } else {
                response.send(data);
            }
        }
    })
})



//添加用户订单详细信息
express.get("/getPerson", (request, response) => {
    let p = request.query;
    sql.query(`INSERT INTO shoptask (userName,shopId,shopName,shopPrice,tell,addresss,detailsAddress) VALUES ("${p.userName}","${p.shopId}","${p.shopName}","${p.shopPrice}","${p.tell}","${p.addresss}","${p.detailsAddress}")`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data);
            console.log(`${p.userName}添加商品${p.shopName}成功`)
        }
    })
})

//接收前端编辑个人信息请求
express.get("/editPerson", (request, response) => {
    let e = request.query;
    sql.query(`UPDATE shoptask SET userName = "${e.userName}",tell = "${e.tell}",detailsAddress = "${e.detailsAddress}" WHERE shopId="${e.shopId}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send("success");
            console.log(`编辑用户${e.userName}信息操作成功`)
        }
    })
})

//接收前端查看个人信息的请求
express.get("/postInfo", (request, response) => {
    sql.query(`SELECT userName,tell FROM shoptask WHERE shopId="${request.query.shopId}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data[0]);
        }
    })
})

express.use(expressStatic(__dirname + "/static"));
express.listen(port);
console.log(`running at ${port}`)